<?php

class DateTimeUtil {

    public static function getCurrentDate($format) {
        return date($format);
    }

    //dd/mm/yyyy
    public static function checkDate($date) {
        $date = trim($date);
        if (empty($date)) {
            return false;
        }

        $date_array = explode("/", $date);
        $day = isset($date_array[0]) ? $date_array[0] : - 1;
        $month = isset($date_array[1]) ? $date_array[1] : - 1;
        $year = isset($date_array[2]) ? $date_array[2] : - 1;

        if (!is_numeric($day) || !is_numeric($month) || !is_numeric($year))
            return FALSE;

        if ($day < 1 || $day > 31)
            return false;
        if ($month < 1 || $month > 12)
            return false;
        if ($year < 0 || $year > 10000)
            return false;

        if (checkdate($month, $day, $year)) {
            return true;
        }
        else {
            return false;
        }
    }

    //dd/mm/yyyy
    public function convertDateToDateSQL($date_convert) {
        if($date_convert == null){
            return null;
        }
        $pos = strpos($date_convert, '/');
        if ($pos === false)
            return $date_convert;

        list($day, $month, $year) = explode("/", $date_convert);

        if (($year == "1970") && ($month == "01" || $month == "1") && ($day == "01" || $day == "1"))
            return "1970-01-01";

        if (!is_numeric($day) || !is_numeric($month) || !is_numeric($year))
            return "";

        if ($day < 1 || $day > 31)
            return "";
        if ($month < 1 || $month > 12)
            return "";
        if ($year < 0 || $year > 10000)
            return "";

        //if (check_date($date_convert))
        return date("Y-m-d", mktime(0, 0, 0, intval($month), intval($day), intval($year)));

    }

    public function convertDateSQLToDate($date_convert) {
        if($date_convert == null){
            return null;
        }
         return date('d/m/Y', strtotime($date_convert));
    }

    public function convertDateTimeSQLToDateTime($date_convert) {
        if($date_convert == null){
            return null;
        }
         return date('d/m/Y h:i:s', strtotime($date_convert));
    }
}
?>
